package com.mfg.controller;

import com.mfg.pojo.Wallet;
import com.mfg.pojo.WalletDetails;
import com.mfg.service.WalletDetailsService;
import com.mfg.service.WalletService;
import com.mfg.util.CommonResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
public class WalletDetailsController {

    @Resource
    private WalletService walletService;

    @Resource
    private WalletDetailsService walletDetailsService;

    /**
     * todo 用户消费100元,
     *      这里还应该有商品id,业务不熟悉，就只写用户消费100元的需求
     * @param userId  用户id
     * @param decreaseMoney  消费金额，这里是100
     * @param payMethod 支付方式：0：钱包 ； 1：银行卡
     * @return
     */
    @Transactional
    @PostMapping("/consumeHundred")
    public CommonResult consumeHundred(@Param("userId") Integer userId,
                                       @RequestParam("decreaseMoney") BigDecimal decreaseMoney,
                                       @RequestParam(value = "payMethod", defaultValue = "0") String payMethod){
        //todo 判断支付方式
        if("0".equals(payMethod)){
            // 钱包消费 100元，访问用 decreaseMoney = 100进行访问
            boolean walletResult = walletService.consumeHundred(userId, decreaseMoney);
            if(!walletResult){
                return new CommonResult(12,"交易失败，余额不足");
            }
            // 插入钱包消费详情
            Wallet wallet = walletService.getMoneyAndIdByUserId(userId);
            WalletDetails walletDetails = new WalletDetails(null,wallet.getWalletId(), wallet.getMoney(),decreaseMoney,"1", "0");
            walletDetailsService.insertWalletDetails(walletDetails);
            return new CommonResult(200, "钱包支付成功", walletDetails);
        }else{
            // 银行卡消费的service
            return new CommonResult(200, "银行卡支付成功");
        }
    }


    /**
     *  todo 退款20元
     *
     * 不熟悉业务，这里需要两个必要参数， userId 和 退款金额，所以用 Wallet对象接收
     *
     * @param wallet
     * @param payMethod
     * @return
     */
    @Transactional
    @PostMapping("/refund")
    public CommonResult refund(Wallet wallet,
                               @RequestParam(value = "payMethod",required = false,defaultValue = "2") String payMethod){
        if("0".equals(payMethod)){
            // 退款到钱包
            walletService.refundToWallet(wallet);

            Wallet w = walletService.getMoneyAndIdByUserId(wallet.getUserId());

            WalletDetails walletDetails = new WalletDetails(null,w.getWalletId(),w.getMoney(),wallet.getMoney(), "0", "2");

            walletDetailsService.insertWalletDetails(walletDetails);

            //todo 商品库存退回

            return new CommonResult(200, "退款到钱包成功", walletDetails);
        }else{
            return new CommonResult(200, "退款到银行卡");
        }
    }


    /**
     *   todo 查询用户钱包金额变动明细
     * @param walletId
     * @return
     */
    @PostMapping("/walletChangeDetails")
    public CommonResult getWalletDetailsChangeByWalletId(@Param("walletId") Integer walletId){
        List<WalletDetails> details = walletDetailsService.getWalletDetailsByWalletId(walletId);
        if(!CollectionUtils.isEmpty(details)){
            return new CommonResult(200, "walletId = " +walletId + " 的消费流水 查询成功", details);
        }
        return new CommonResult(200, "暂无消费记录");
    }
}
